package com.luobu.javaee.controller;

import com.luobu.javaee.domain.Document;
import com.luobu.javaee.domain.dto.DocumentDto;
import com.luobu.javaee.domain.dto.DocumentResDto;
import com.luobu.javaee.domain.dto.MultiKeywordSearchDto;
import com.luobu.javaee.service.DocumentElasticSearchService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/search")
@Setter(onMethod_ = @Autowired)
public class SearchController {
    private DocumentElasticSearchService documentElasticSearchService;

    /*
    * 测试接口 误用
    * */
    @GetMapping("/docs")
    public ResponseEntity<List<DocumentResDto>> searchDocs(@RequestParam(value = "docName", defaultValue = "") String docName){
        List<DocumentResDto> res = documentElasticSearchService.findDocByDocName(docName);
        return ResponseEntity.ok(res);
    }

    /*
    * 根据用户id和文档名称返回固定查询结果
    * */
    @GetMapping("/baseSearch")
    public ResponseEntity<List<DocumentResDto>> baseSearch(@RequestParam(value = "docName", defaultValue = "") String docName,
                                                           @RequestParam(value = "userId") Long userId){
        List<DocumentResDto> res = documentElasticSearchService.findDocByDocNameAndUserId(docName, userId);
        return ResponseEntity.ok(res);
    }

    /*
    * 上传一条记录
    * */
    @PostMapping("/upload")
    public ResponseEntity<Void> upload(@RequestBody DocumentDto documentDto){
        documentElasticSearchService.upload(documentDto);
        return ResponseEntity.ok().build();
    }

    /*
    * 根据文档id删除记录
    * */
    @PostMapping("/deleteByDocId")
    public ResponseEntity<Void> deleteByDocId(@RequestParam(value = "docId") Long docId){
        documentElasticSearchService.deleteByDocId(docId);
        return ResponseEntity.ok().build();
    }

    /*
     * 根据文档id删除记录
     * */
    @PostMapping("/deleteByUserIdAndDocName")
    public ResponseEntity<Void> deleteByUserIdAndDocName(@RequestParam(value = "userId") Long userId,
                                                         @RequestParam(value = "docName") String docName){
        documentElasticSearchService.deleteByUidAndDocName(userId, docName);
        return ResponseEntity.ok().build();
    }

    /*
    * 多关键词搜索
    * */
    @PostMapping("/multikeywords")
    public ResponseEntity<List<DocumentResDto>> searchByMultiKeywords(@RequestBody MultiKeywordSearchDto multiKeywordSearchDto){
        List<String> keywords = multiKeywordSearchDto.getKeywords();
        Long userId = multiKeywordSearchDto.getUserId();
        List<DocumentResDto> documentResDtoList = documentElasticSearchService.multiKeywordsSearch(keywords, userId);

        return ResponseEntity.ok(documentResDtoList);
    }

    @PostMapping("/searchinarchive")
    public ResponseEntity<List<DocumentResDto>> searchInArchive(@RequestParam(value = "keyword") String keyword,
                                                                @RequestParam(value = "archiveid") Long archiveId) {
        return ResponseEntity.ok(documentElasticSearchService.searchInArchive(keyword, archiveId));
    }

}
